﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.ComponentModel;

namespace WebPALTT.Lib
{
    /*
     * Class: ActionItem_WebRequest
     * 
     * This class represents a web request action and inherits from @link[ActionItem.cs.htm, ActionItem].
     */
    [Serializable()]
    public class ActionItem_WebRequest : ActionItem
    {
        [CategoryAttribute("Action Properties"), ReadOnlyAttribute(true)]
        public override ActionType ActionType { get { return ActionType.WebRequest; } }

        String mMethod;
        Uri mUri;
        String mHttpVersion;
        KeyValuePair<String, String>[] mHeaders;
        String mContent;

        #region Properties
        /*
         * Name: Properties
         * 
         * {{/string/ Method}}:
         * 
         * Gets or set the Http method (e.g. GET, POST, ...).
         * 
         * /Uri/ Uri:
         * 
         * Gets or set the Uri of the request as a System.Uri object.
         * 
         * /string/ HttpVersion:
         * 
         * Gets or set the http protocol version (Only 1.0 and 1.1 are supported) as a string.
         * 
         * /string/ Headers:
         * 
         * Gets or set the array of http headers for the request as key-value pairs.
         * Uses an array of KeyValuePair<String, String> objects where the key is the name of the header and the value is the header value.
         * 
         * /string/ Content:
         * 
         * Gets or set the body of the http request (only available for POST requests) as a string.
         */

        /// <summary>
        /// Gets or set the Http method (e.g. GET, POST, ...).
        /// </summary>
        public String Method
        {
            get { return mMethod; }
            set { mMethod = value; }
        }

        /// <summary>
        /// Gets or set the Uri of the request.
        /// </summary>
        public Uri Uri
        {
            get { return mUri; }
            set { mUri = value; }
        }

        /// <summary>
        /// Gets or set the http protocol version (Only 1.0 and 1.1 are supported).
        /// </summary>
        public String HttpVersion
        {
            get { return mHttpVersion; }
            set { mHttpVersion = value; }
        }

        /// <summary>
        /// Gets or set the array of http headers for the request as key-value pairs.
        /// </summary>
        public KeyValuePair<String, String>[] Headers
        {
            get { return mHeaders; }
            set { mHeaders = value; }
        }

        /// <summary>
        /// Gets or set the body of the http request (only available for POST requests). 
        /// </summary>        
        [Editor(typeof(System.ComponentModel.Design.MultilineStringEditor), typeof(System.Drawing.Design.UITypeEditor))]
        public String Content
        {
            get { return mContent; }
            set { mContent = value; }
        }
        #endregion

        /// <summary>
        /// Creates a new web request action item.
        /// </summary>
        /// <param name="method">The method for the request</param>
        /// <param name="uri">The Uri of the request</param>
        /// <param name="httpVersion">The http protocol version</param>
        /// <param name="headers">List of headers for the request</param>
        /// <param name="content">The body of the request</param>
        public ActionItem_WebRequest(String name, String method, Uri uri, String httpVersion, KeyValuePair<String, String>[] headers, String content)
            : base(name)
        {
            mMethod = method;
            mUri = uri;
            mHttpVersion = httpVersion;
            mHeaders = headers;
            mContent = content;
        }

        public ActionItem_WebRequest(String name) : this(name, "GET", null, "1.0", new KeyValuePair<string, string>[] { }, "") { }

        public ActionItem_WebRequest(SerializationInfo info, StreamingContext ctxt)
            : base(info, ctxt)
        {
            mMethod = (string)info.GetValue("Method", typeof(string));
            mUri = (Uri)info.GetValue("Uri", typeof(Uri));
            mHttpVersion = (string)info.GetValue("HttpVersion", typeof(string));
            mHeaders = (KeyValuePair<String, String>[])info.GetValue("Headers", typeof(KeyValuePair<String, String>[]));
            mContent = (string)info.GetValue("Content", typeof(string));
        }

        public override void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            base.GetObjectData(info, ctxt);
            info.AddValue("Method", mMethod);
            info.AddValue("Uri", mUri);
            info.AddValue("HttpVersion", mHttpVersion);
            info.AddValue("Headers", mHeaders);
            info.AddValue("Content", mContent);
        }

        public override string[] Validate()
        {
            List<string> errors = new List<string> { };
            if (Name.Equals("Then") || Name.Equals("Else") || Name.Equals("Actions"))
                errors.Add("Error: action[" + Name + "(" + ActionType + ")] is using a reserverd name.");

            return errors.ToArray();
        }

        public override ActionItem GetChild(Guid key) { return null; }
    }
}
